﻿using BCCommon;
using BCEntity.Common.UploadFileRecord;
using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;

namespace BCData.Common.UploadFileRecord
{
    public class UploadFileRecordData : IUploadFileRecordData
    {
        private const string QUERY = @"SELECT UploadFileRecordNo,FileSize,FilePath,FileUrl,FileSuffix,RecordTime,FileUsage,RemarkType,
                                            WebsiteName,WebsiteFileDirectory,WebsiteFullName,FileType
                                        FROM UploadFileRecord";
        private readonly IDatabaseContext databaseContext;

        public UploadFileRecordData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }

        public async Task<UploadFileRecordEntity> Add(UploadFileRecordEntity uploadFileRecordEntity)
        {
            string sql = @"INSERT INTO UploadFileRecord(UploadFileRecordNo,FileSize,FilePath,FileUrl,FileSuffix,FileUsage,RemarkType,WebsiteName,WebsiteFileDirectory,WebsiteFullName,FileType)
                            VALUES(?UploadFileRecordNo,?FileSize,?FilePath,?FileUrl,?FileSuffix,?FileUsage,?RemarkType,?WebsiteName,?WebsiteFileDirectory,?WebsiteFullName,?FileType);
                            {0} WHERE UploadFileRecordNo=?UploadFileRecordNo;";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?UploadFileRecordNo", uploadFileRecordEntity.UploadFileRecordNo, DbType.AnsiString, size: 22);
            parameters.Add("?FileSize", uploadFileRecordEntity.FileSize, DbType.Int64, size: 20);
            parameters.Add("?FilePath", uploadFileRecordEntity.FilePath, DbType.AnsiString, size: 1000);
            parameters.Add("?FileUrl", uploadFileRecordEntity.FileUrl, DbType.AnsiString, size: 1000);
            parameters.Add("?FileSuffix", uploadFileRecordEntity.FileSuffix, DbType.AnsiString, size: 10);
            parameters.Add("?FileUsage", uploadFileRecordEntity.FileUsage, DbType.AnsiString, size: 30);
            parameters.Add("?RemarkType", uploadFileRecordEntity.RemarkType, DbType.AnsiString, size: 50);
            parameters.Add("?WebsiteName", uploadFileRecordEntity.WebsiteName, DbType.AnsiString, size: 50);
            parameters.Add("?WebsiteFileDirectory", uploadFileRecordEntity.WebsiteFileDirectory, DbType.AnsiString, size: 1000);
            parameters.Add("?WebsiteFullName", uploadFileRecordEntity.WebsiteFullName, DbType.AnsiString, size: 1000);
            parameters.Add("?FileType", uploadFileRecordEntity.FileType, DbType.AnsiString, size: 30);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<UploadFileRecordEntity>(string.Format(sql, QUERY), parameters, databaseContext.Transaction);
        }

        public async Task<UploadFileRecordEntity> GetEntity(string uploadFileRecordNo)
        {
            string sql = @"{0} WHERE UploadFileRecordNo=?UploadFileRecordNo;";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?UploadFileRecordNo", uploadFileRecordNo, DbType.AnsiString, size: 22);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<UploadFileRecordEntity>(string.Format(sql, QUERY), parameters);
        }

        public async Task<bool> Delete(string uploadFileRecordNo)
        {
            string sql = @"DELETE FROM UploadFileRecord WHERE UploadFileRecordNo=?UploadFileRecordNo;";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?UploadFileRecordNo", uploadFileRecordNo, DbType.AnsiString, size: 22);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) >0;
        }

        public async Task<IEnumerable<UploadFileRecordEntity>> GetNoneTransportListAsync()
        {
            string sql = string.Format("{0} WHERE IsTransported=0", QUERY);
            return await this.databaseContext.Slave.QueryAsync<UploadFileRecordEntity>(sql);
        }

        public async Task<bool> UpdateTransportedAsync(string uploadFileRecordNo)
        {
            string sql = @"UPDATE UploadFileRecord SET IsTransported=1 WHERE UploadFileRecordNo=?UploadFileRecordNo;";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?UploadFileRecordNo", uploadFileRecordNo, DbType.AnsiString, size: 22);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }

        public async Task<(IEnumerable<UploadFileRecordEntity>, int)> GetUploadFileRecords(string fileType, int index, int size)
        {
            string sql = string.Format("{0} WHERE FileType=?FileType ORDER BY RecordTime DESC Limit {1},{2};SELECT COUNT(1) FROM UploadFileRecord WHERE FileType=?FileType;", QUERY, index, size);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?FileType", fileType, DbType.AnsiString, size: 30);
            return await this.databaseContext.Slave.QueryMultipleAsync<UploadFileRecordEntity,int>(sql, parameters);
        }
    }
}
